package com.biz.primus.service.push.config;

import cn.jpush.api.JPushClient;
import com.xiaomi.xmpush.server.Sender;
import lombok.Setter;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author david-liu
 */
@Configuration
@ConfigurationProperties(prefix = "push.setting")
@Setter
public class PushConfig {

    private String jpushMasterSecret;

    private String jpushAppKey;

    private String miAppSecret;

    private String rabbitHost;

    private Integer rabbitPort;

    private String rabbitUsername;

    private String rabbitPassword;

    @Bean
    JPushClient jPushClient() {
        return new JPushClient(jpushMasterSecret, jpushAppKey);
    }

    @Bean(name = "miPushSender")
    Sender miPushSender() {
        return new Sender(miAppSecret);
    }

    @Bean
    ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setQueueCapacity(200);
        threadPoolTaskExecutor.setCorePoolSize(5);
        threadPoolTaskExecutor.setMaxPoolSize(100);
        threadPoolTaskExecutor.setKeepAliveSeconds(300);
        return threadPoolTaskExecutor;
    }

    @Bean
    ConnectionFactory connectionFactory() {
        com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = new com.rabbitmq.client.ConnectionFactory();
        rabbitConnectionFactory.setHost(this.rabbitHost);
        rabbitConnectionFactory.setPort(this.rabbitPort);
        rabbitConnectionFactory.setUsername(this.rabbitUsername);
        rabbitConnectionFactory.setPassword(this.rabbitPassword);
        return new CachingConnectionFactory(rabbitConnectionFactory);
    }

    @Bean
    MessageListenerContainer messageListenerContainer() {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter();
        return null;
    }

}
